﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TextEditor.TextDocument.Document;
using System.Drawing.Printing;

namespace TextEditor.TextDocument.Control
{
    public partial class frmSetPage : Form
    {
        private ZYTextDocument document;

        public ZYTextDocument Document
        {
            get { return document; }
            set { document = value; }
        }

        public frmSetPage()
        {
            InitializeComponent();
        }

        public frmSetPage(ZYTextDocument _document)
        {
            this.document = _document;
            InitializeComponent();
            PrinterSettings mySetting = new PrinterSettings();
            foreach (PaperSize size in mySetting.PaperSizes)
            {
                this.comboBox1.Items.Add(size.PaperName);
            }
            this.comboBox1.Text = document.Pages.paperSizeName;// this.comboBox1.FindString(this.document.Pages.PaperKindName);
            //this.numericUpDown5.Value = ZYTextDocument.GetAnInchOfTheThousandths(document.Pages.PaperWidth);
            //this.numericUpDown6.Value = ZYTextDocument.GetAnInchOfTheThousandths(document.Pages.PaperHeight);
            this.numericUpDown1.Value = ZYTextDocument.GetAnInchOfTheThousandths(document.Pages.LeftMargin);
            this.numericUpDown2.Value = ZYTextDocument.GetAnInchOfTheThousandths(document.Pages.RightMargin);
            this.numericUpDown3.Value = ZYTextDocument.GetAnInchOfTheThousandths(document.Pages.TopMargin);
            this.numericUpDown4.Value = ZYTextDocument.GetAnInchOfTheThousandths(document.Pages.BottomMargin);
            this.checkBox1.Checked = document.Pages.EnablePageHeader;
            this.checkBox2.Checked = document.Pages.EnablePageFooter;
            this.checkBox3.Checked = document.Pages.Landscape;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            document.Pages.EnablePageHeader = checkBox1.Checked;
            document.Pages.EnablePageFooter = checkBox2.Checked;

            //this.numericUpDown5.Value = ZYTextDocument.GetAnInchOfTheThousandths(document.Pages.PaperWidth);
            //this.numericUpDown6.Value = ZYTextDocument.GetAnInchOfTheThousandths(document.Pages.PaperHeight);
            document.Pages.PaperKindName = this.comboBox1.Text;
            document.Pages.PaperWidth = Convert.ToInt32(this.numericUpDown5.Value);
            document.Pages.PaperHeight = Convert.ToInt32(this.numericUpDown6.Value);
            document.Pages.LeftMargin = ZYTextDocument.GetTheThousandthsOfAnInch(Convert.ToInt32(this.numericUpDown1.Value));
            document.Pages.RightMargin = ZYTextDocument.GetTheThousandthsOfAnInch(Convert.ToInt32(this.numericUpDown2.Value));
            document.Pages.TopMargin = ZYTextDocument.GetTheThousandthsOfAnInch(Convert.ToInt32(this.numericUpDown3.Value));
            document.Pages.BottomMargin = ZYTextDocument.GetTheThousandthsOfAnInch(Convert.ToInt32(this.numericUpDown4.Value));
            document.Pages.Landscape = checkBox3.Checked;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            PrinterSettings mySetting = new PrinterSettings();
            string name = this.comboBox1.Text;
            foreach (PaperSize size in mySetting.PaperSizes)
            {
                if (name == size.PaperName)
                {
                    this.numericUpDown5.Value = size.Width;
                    this.numericUpDown6.Value = size.Height;
                    break;
                }
            }
        }
    }
}
